// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.output.features;

public class BasicEscaper implements Escaper {
    
    public String escapeAttributeValue(String text) {
        return escape(text, true);
    }

    public String escapeContent(String text) {
        return escape(text, false);
    }

    private String escape(String text, boolean escapeQuotes) {
        StringBuffer buffer = new StringBuffer();
        for (char character : text.toCharArray()) {
            switch (character) {
                case '"':
                    if (escapeQuotes) {
                        buffer.append("&quot;");
                    } else {
                        buffer.append(character);
                    }
                    break;
                case '<':
                    buffer.append("&lt;");
                    break;
                case '>':
                    buffer.append("&gt;");
                    break;
                case '&':
                    buffer.append("&amp;");
                    break;
                default:
                    buffer.append(character);
            }
        }
        return buffer.toString();
    }
}
